package com.longrise.co.ui.mine.fragment;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.ui.mine.adapter.EvaluationAdapter;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.MineEvaluationBean;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;

import butterknife.Bind;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by luoyang on 2017-7-21.
 */

public class MineEvaluationFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<MineEvaluationBean>, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @Bind(R.id.main)
    LinearLayout main;
    @Bind(R.id.search_no_data_tv)
    TextView searchNoDataTv;
    @Bind(R.id.no_data_tip)
    RelativeLayout noDataTip;

    private EvaluationAdapter mAdapter;

    private int mPageCount = 0;
    private int mPageNum = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.common_recylerview_nodata;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "我的评价");
        main.setBackgroundColor(Color.parseColor("#1A098EDD"));
        searchNoDataTv.setText("未查询到评价信息");

        initRecyclerView();
        mRxManager.on(Constants.RX_EVENT_EVALUATION_DETAIL, new Action1<MineEvaluationBean.ListBean>() {
            @Override
            public void call(MineEvaluationBean.ListBean bean) {
                if(bean != null) {
                    start(EvaluationDetailFragment.newInstance(bean.getEvaluateid(), bean.getAreaid()));
                }
            }
        });
        noDataTip.setVisibility(View.GONE);
        mPresenter.getEvaluationList(null, mPageNum, true);
    }

    private void initRecyclerView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Constants.RECYCLER_RGB);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new EvaluationAdapter(null);

        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(ApiConstants.PAGE_SIZE_10, true);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void requestSuccess(MineEvaluationBean info) {
        if (info != null && info.getList() != null && info.getList().size() > 0) {

            mPageCount = Integer.parseInt(info.getTotalnum());
            if (mPageNum == 1) {
                //刷新
                mAdapter.setNewData(info.getList());
            } else {
                //加载更多
                mAdapter.notifyDataChangedAfterLoadMore(info.getList(), true);
            }
        } else {
            //没有搜索到结果
            noDataTip.setVisibility(mPageNum == 1 ? View.VISIBLE : View.GONE);
            mPageNum--;
            mPageCount = mPageNum;
            mAdapter.notifyDataChangedAfterLoadMore(false);
            if (mPageNum > 1) {
                showLongToast("全部加载完毕");
            }
        }
    }

    @Override
    public void onRequestComplete() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }

    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    @Override
    public void onRefresh() {
        mPageNum = 1;
        noDataTip.setVisibility(View.GONE);
        mAdapter.notifyDataChangedAfterLoadMore(true);
        mPresenter.getEvaluationList(null, mPageNum, false);
    }

    public static MineEvaluationFragment newInstance() {
        return new MineEvaluationFragment();
    }

    @Override
    public void onItemClick(View view, int i) {
        MineEvaluationBean.ListBean bean = mAdapter.getItem(i);
        if(bean != null) {
            start(EvaluationDetailFragment.newInstance(bean.getEvaluateid(), bean.getAreaid()));
        }
    }

    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (mPageNum < mPageCount) {
                    //还有更多数据
                    mPresenter.getEvaluationList(null, ++mPageNum,false);
                } else {
                    mAdapter.notifyDataChangedAfterLoadMore(false);
                }
            }

        }, 2000);
    }
}
